package io.github.yuazer.zpokespring.Listener;

import com.pixelmonmod.pixelmon.api.pokemon.Pokemon;
import com.pixelmonmod.pixelmon.api.storage.StorageProxy;
import io.github.yuazer.zaxlib.Utils.YamlUtils;
import io.github.yuazer.zpokespring.Main;
import io.github.yuazer.zpokespring.MyMapUtils.MapUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author YuaZer[QQ:1109132]
 */
public class SpringListener {
    final static ConcurrentHashMap<UUID, String> playerMap = new MapUtils();

    public ConcurrentHashMap<UUID, String> getPlayerMap() {
        return playerMap;
    }

    public void isInSpring(Player player) {
        Location playerLocation = player.getLocation();
        for (String springName : Main.getInstance().getConfig().getConfigurationSection("SpringSetting.").getKeys(false)) {
            /**
             * 获取位置
             * */
            String world = Main.getInstance().getConfig().getString("SpringSetting." + springName + ".world");
            String x1z1 = Main.getInstance().getConfig().getString("SpringSetting." + springName + ".x1z1");
            String x2z2 = Main.getInstance().getConfig().getString("SpringSetting." + springName + ".x2z2");
            int x1 = Integer.parseInt(x1z1.split("//")[0]);
            int z1 = Integer.parseInt(x1z1.split("//")[1]);
            int x2 = Integer.parseInt(x2z2.split("//")[0]);
            int z2 = Integer.parseInt(x2z2.split("//")[1]);
            if (x1 != x2 && z1 != z2) {
                int maxX = Math.max(x1, x2), minX = Math.min(x1, x2), maxZ = Math.max(z1, z2), minZ = Math.min(z1, z2);
                /**
                 * 判断是否处在温泉范围内
                 * */
                if (playerLocation.getBlockX() <= maxX
                        && playerLocation.getBlockX() >= minX
                        && playerLocation.getBlockZ() <= maxZ
                        && playerLocation.getBlockZ() >= minZ
                        && playerLocation.getWorld().getName().equalsIgnoreCase(world)) {
                    playerMap.put(player.getUniqueId(), springName);
                    return;
                } else {
                    playerMap.remove(player.getUniqueId(), springName);
                }
            }
        }
    }

    public void getExp() {
        for (Player player : Bukkit.getOnlinePlayers()) {
            isInSpring(player);
            try {
                /**
                 * 判断玩家所在温泉不为空
                 * */
                if (!(playerMap.get(player.getUniqueId()) == null && playerMap.get(player.getUniqueId()).isEmpty())) {
                    String springName = this.getPlayerMap().get(player.getUniqueId());
                    int exp = YamlUtils.getConfigInt("SpringSetting." + springName + ".exp");
                    if (Main.getInstance().getConfig().getString("SpringSetting." + springName + ".expMode").equalsIgnoreCase("SINGLE")) {
                        Pokemon pokemon = StorageProxy.getParty(player.getUniqueId()).get(0);
                        if (!pokemon.isEgg() && pokemon.getPokemonLevel() != 100 && pokemon != null) {
                            pokemon.setExperience(pokemon.getExperience() + exp);
                            runCommands(player, springName, exp);
                        }
                    } else if (Main.getInstance().getConfig().getString("SpringSetting." + springName + ".expMode").equalsIgnoreCase("ALL")) {
                        for (Pokemon pokemon : StorageProxy.getParty(player.getUniqueId()).getAll()) {
                            if (!pokemon.isEgg() && pokemon != null) {
                                if (pokemon.getPokemonLevel() != 100) {
                                    pokemon.setExperience(pokemon.getExperience() + exp);
                                }
                            }
                        }
                        runCommands(player, springName, exp);
                    } else {
                        return;
                    }
                }
            } catch (NullPointerException ignored) {
            }
        }
    }

    private void runCommands(Player player, String springName, int exp) {
        for (String cmd : Main.getInstance().getConfig().getStringList("SpringSetting." + springName + ".commands")) {
            if (cmd != null && !cmd.equalsIgnoreCase("")) {
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%player%", player.getName()));
            }
        }
        player.sendMessage(YamlUtils.getConfigMessage("Message.getExp").replace("%exp%", String.valueOf(exp)));
    }
}